/* Solution by - Alec William Yeasting
 * Project Euler - problem 16:
 *
 *  2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
 *  What is the sum of the digits of the number 2^1000?
 */
#include <iostream>
#include <vector>
#include <time.h>



std::vector<int> timesTwo(std::vector<int> a);//returns the vector result of an int vector multiplied by two
std::vector<int> twoToThePowerOfN(int n);//returns the vector result of 2^n
int sumVector(std::vector<int> a);//returns the sum of the integers in a vector

int main(){

    /// just a comment
    // Start execution time timer -------------------------------
        clock_t start, end;                                    //
        double time;                                           //
        start = clock();                                       //
    // ----------------------------------------------------------

    std::vector<int> a = twoToThePowerOfN(1000);
    std::cout << sumVector(a) << std::endl;

    // End execution time timer ---------------------------------
        end = clock();                                         //
        time = (double)(end - start)/CLOCKS_PER_SEC;           //
        std::cout << "Executed in: " << time << " seconds.\n"; //
    // ----------------------------------------------------------
}

/** \brief multiplies a integer vector by two
 *
 * \param a vector to multiply by two
 * \return the result of the vector multiplication
 *
 */
std::vector<int> timesTwo(std::vector<int> a){

    int carry = 0;/**<  the digit being carried from the last opertion*/
    int carry1 = 0;//the digit being carried from the current operation
    int num;//the i integer of vector a
    std::vector<int>::iterator it;

    for(int i = a.size()-1; i>-1; i--){//multiplies vector a by two and applies any integer carry operations

        num = a[i];

        if(num*2>9){
            carry1 = num*2/10;
            a[i] = num*2 % 10 + carry;
        }else{
            carry1 = 0;
            a[i] = num*2 + carry;
        }
        if(i==0 && carry1>0){
            it = a.begin();
            a.insert(it,1);
        }
        carry = carry1;
    }
    return a;
}
/** \brief fdasfa
 *
 * \param adfsafc
 * \param afdsaf
 * \return adsffafa
 *
 */
std::vector<int> twoToThePowerOfN(int n){
    std::vector<int> result (1,1);
    for(int i = 0; i<n; i++){
        result = timesTwo(result);
    }
    return result;
}
/** \brief yo sup sdhafoids
 *
 * \param fadsjkfhas
 * \return fdajfdjalkfjdsa
 *
 */
int sumVector(std::vector<int> a){
    int sum = 0;
    for(int i=0; i<a.size(); i++){sum += a[i];}
    return sum;
}
